//app.js
var aldstat = require("./utils/ald-stat.js");
var util = require('./utils/md5.js');
App({
  onLaunch: function (o) {
    console.log(o);

    var regiment_userid = typeof (o.query.regiment_userid) == 'undefined' ? 0 : o.query.regiment_userid
    var regiment_share_userid = typeof (o.query.regiment_share_userid) == 'undefined' ? 0 : o.query.regiment_share_userid
    if (regiment_userid !== 0) {
      wx.setStorage({
        key: "regiment_userid",
        data: regiment_userid
      })

      wx.setStorage({
        key: "share_goods_id",
        data: o.query.id
      })

    }
    if (regiment_share_userid !== 0) {
      wx.setStorage({
        key: "regiment_share_userid",
        data: regiment_share_userid
      })
    }


    //调用API从本地缓存中获取数据
    // var logs = wx.getStorageSync('logs') || []
    // logs.unshift(Date.now())'1497510722ca0ac309438ec82387fc82175bc5ae7b'
    // wx.setStorageSync('logs', logs)

    /*if (this.url =='http://xcx.cn/index.php/'){
      console.log('5555')
      wx.setStorage({
        key: "token_key",
        data: '1497510722ca0ac309438ec82387fc82175bc5ae7b'
      })
      }*/

    //this.login_value(o);

		/*    wx.getUserInfo({
							success: function (res1) {
		console.log(res1.userInfo)
		
							}})
		return*/

    //this.getUserInfo(function(token_key){ })

  },
  //用户信息加载请求
  load: function (res_fun) {
    var that = this;
    var openId = (wx.getStorageSync('openid'));
    wx.removeStorage({ key: 'blessing_text' });
    if (openId) {
      wx.getUserInfo({
        success: function (res) {
          console.log(res);
          wx.setStorage({ key: "nickname", data: res.userInfo.nickName })//缓存用户名称
          wx.setStorage({ key: "avatarurl", data: res.userInfo.avatarUrl })//缓存用户头像
        },
        fail: function () {
          console.log("获取失败！")
        },
      })
    } else {
      wx.login({
        success: function (res) {
          if (res.code) {
            wx.getUserInfo({
              withCredentials: true,
              success: function (res_user) {
                wx.showLoading({ title: 'loading', mask: true })
                wx.request({
                  //后台接口地址
                  url: that.url2 + 'api/userLogin',
                  data: {
                    code: res.code,
                    encryptedData: res_user.encryptedData,
                    iv: res_user.iv,
                    // token: 'abc'
                  },
                  method: 'GET',
                  success: function (res) {
                    console.log(res);
                    // wx.setStorage({ key: "Appid", data: res.data.appid })//缓存小程序Appid
                    // wx.setStorage({ key: "Secret", data: res.data.Secret })//缓存小程序Secret
                    wx.setStorage({ key: "nickname", data: res.data.username })//缓存用户名称
                    wx.setStorage({ key: "avatarurl", data: res.data.head_ico })//缓存用户头像
                    wx.setStorage({ key: "openid", data: res.data.open_id })//缓存用户 openID
                    // wx.setStorage({ key: "token", data: res.data.api_token })//缓存用户 api_token

                    res_fun(res.data.openid);
                    that.token_md5();
                    wx.hideLoading();

                  },
                  fail: function (res) {
                    wx.hideLoading();
                    console.log(res);
                    console.log("获取失败！")
                    wx.showModal({
                      title: '提示',
                      content: '网络连接失败',
                    })
                  },
                })
              }
            })
          }
        }
      })
    }
  },
  // token加密处理
  token_md5: function () {
    console.log('加密处理！！');
    var oDate = new Date(); //实例一个时间对象；
    var yue = oDate.getMonth() + 1,
      hour = oDate.getHours(),
      day = oDate.getDate();
    yue = (('' + yue).length == 2 ? yue : ('0' + yue));
    day = (('' + day).length == 2 ? day : ('0' + day));
    hour = (('' + hour).length == 2 ? hour : ('0' + hour));
    var token = 'camelLongLive' + oDate.getFullYear() + yue + day + hour;
    console.log(token);
    wx.setStorage({ key: "token_md5", data: util.hexMD5(token) })//缓存加密token_md5
    console.log(wx.getStorageSync('token_md5'));
  },


  globalData: {
    userInfo: null,
    token_key: null,
  },

  shopName: '小程序-骆驼官方商城',
  // debug: true, 
  url: 'https://jm.camel.com.cn/',//真会说话
  url2: 'https://hd.huwaishequ.com/kxred/public/',//线上
  // url: 'https://lite5.camel.com.cn/index.php/',//晓镇哥
  // url: 'http://wxappshop.top/index.php/',//weiki
  // url: 'http://172.18.41.33/index.php/',//杰哥
  // url: 'http://172.18.41.25/wxappshop/',//谦哥
  // urlimg: 'https://lite2.camel.com.cn/',

  urlimg: 'https://lteimg1.camel.com.cn/',
  // urlimg: 'http://172.18.41.30/wxappshop/',//谦哥
})